package com.zj.aspects;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.zj.web.bean.MyUserDTO;
import com.zj.web.bean.OrderMessageDTO;
import com.zj.web.bean.OrdersDTO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Log
public class JmsMessageProducter {
    //配置队列名
    @Value("${spring.activemq.queueName}")
    private String queueName;
    @Value("${spring.activemq.verifyCodeQueue}")
    private String verifyCodeQueue;
    @Value("${spring.activemq.orderSuccessQueue}")
    private String orderSuccessQueue;
    @Value("${spring.activemq.orderStatusQueue}")
    private String orderStatusQueue;

    @Autowired
    private JmsTemplate jmsTemplate; //Jms的操作模板  类比jdbcTemplate

    public void sendMessage(MyUserDTO myUserDTO){
        //将对象数据转换为json字符串
        myUserDTO.setUserPassword(null);
//        myUserDTO.setHeadUrl(null);
        myUserDTO.setAvatarFile(null);
        myUserDTO.setOptime(new Date());

        Gson gson = new Gson();
        //将对象中null值的数据排除掉

        String json = gson.toJson(myUserDTO);
        //发送消息到队列
        jmsTemplate.convertAndSend(queueName,json);
        log.info("发送消息到队列：" + queueName + "，消息内容：" + json);
    }
    //发送验证码到队列
    public void sendVerifyCodeMessage(MyUserDTO myUserDTO){
        Gson gson = new Gson();
        //将对象中null值的数据排除掉
        String json = gson.toJson(myUserDTO);
        //发送消息到队列
        jmsTemplate.convertAndSend(verifyCodeQueue,json);
        log.info("发送消息到队列：" + verifyCodeQueue + "，消息内容：" + json);
    }
    //发送订单信息到队列
    public void sendOrderMessage(OrderMessageDTO orderMessageDTO){
        Gson gson = new Gson();
        //将对象中null值的数据排除掉
        String json = gson.toJson(orderMessageDTO);
        //发送消息到队列
        jmsTemplate.convertAndSend(orderSuccessQueue,json);
        log.info("发送消息到队列：" + orderSuccessQueue + "，消息内容：" + json);
    }
    //发送订单状态到队列
    public void sendOrderStatusMessage(OrderMessageDTO orderMessageDTO){
        Gson gson = new Gson();
        //将对象中null值的数据排除掉
        String json = gson.toJson(orderMessageDTO);
        //发送消息到队列
        jmsTemplate.convertAndSend(orderStatusQueue,json);
        log.info("发送消息到队列：" + orderStatusQueue + "，消息内容：" + json);
    }
}
